package com.whfc.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class NumberParser {
    private static final Pattern COMMA_PATTERN = Pattern.compile(",");
    private static final Logger log = LoggerFactory.getLogger(NumberParser.class);

    public static List<Integer> parseCommaSeparatedNumbers(String input) {
        if (input == null || input.isEmpty() || "0".equals(input)) {
            return Collections.emptyList();
        }

        return COMMA_PATTERN.splitAsStream(input)
                .map(String::trim)
                .filter(part -> !part.isEmpty())
                .map(NumberParser::safeParseInt)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    private static Integer safeParseInt(String str) {
        try {
            return Integer.valueOf(str);
        } catch (NumberFormatException e) {
            log.debug("无效数字格式将被忽略: {}", str);
            return null;
        }
    }
}
